import axiosInstance from '../utils/httpUtil'
import { loginWeb, registerWeb, articlesWeb, articlesWeb_write, commentsWeb, replyWeb, draftWeb, homeWeb, homeSettingWeb, postPicWeb, chatWeb } from '../utils/env_config/development_ENV'

const axios = axiosInstance

// ====================首页展示内容接口==================== //
// 获取首页内容
const getMainDisplay = () => {
	return axios({
		url: homeWeb + '1',
		method: 'get'
	})
}
// 更新首页内容
const updateMainDisplay = data => {
	return axios({
		url: homeWeb + '1',
		method: 'patch',
		data: data
	})
}

// ====================首页预设接口==================== //
// 获取首页预设内容
const getHomeDisplay = () => {
	return axios({
		url: homeSettingWeb,
		method: 'get'
	})
}
// 更新首页预设内容
const updateHomeDisplay = (data, num) => {
	if (num) {
		// num判空
		return axios({
			url: homeSettingWeb + num,
			method: 'patch',
			data: data
		})
	}
}
// 新增首页内容预设
const postHomeDisplay = data => {
	return axios({
		url: homeSettingWeb,
		method: 'post',
		data: data
	})
}

// ====================用户信息接口==================== //
// 登录
const login = id => {
	return axios({
		url: loginWeb + id,
		method: 'get'
	})
}
// 注册
const register = (id, name, email, password) => {
	return axios({
		url: registerWeb,
		method: 'post',
		data: {
			id: id,
			name: name,
			email: email,
			password: password
		}
	})
}
// 更新用户信息
const updateUserInfo = (id, userinfo) => {
	return axios({
		url: loginWeb + id,
		method: 'patch',
		data: userinfo
	})
}
// 注销账号
const closeAccount = id => {
	return axios({
		url: loginWeb + id,
		method: 'delete'
	})
}

// ====================获取文章接口==================== //
// 获取所有文章 ---!!!待分页!!!---
const getArticle_all = () => {
	return axios({
		url: articlesWeb,
		method: 'get'
	})
}
// 按文章id获取文章
const getArticle_id = id => {
	return axios({
		url: articlesWeb,
		method: 'get',
		params: {
			id: id
		}
	})
}
// 按作者id获取文章
const getArticle_author = AuthorID => {
	return axios({
		url: articlesWeb,
		method: 'get',
		params: {
			AuthorID: AuthorID
		}
	})
}
// 按分类获取
const getArticle_type = type => {
	return axios({
		url: articlesWeb,
		method: 'get',
		params: {
			type: type
		}
	})
}
// 按tag获取
const getArticle_tag = tag => {
	return axios({
		url: articlesWeb,
		method: 'get',
		params: {
			Tags_like: tag
		}
	})
}
// 按日期(例：2023-01-01 or 2023-01 or 2023)获取
const getArticle_date = date => {
	return axios({
		url: articlesWeb,
		method: 'get',
		params: {
			CreationDate_like: date
		}
	})
}
// 按日期获取最新num篇
const getArticle_new = (num, AuthorID) => {
	return axios({
		url: articlesWeb,
		method: 'get',
		params: {
			AuthorID: AuthorID,
			_sort: 'CreationDate',
			_order: 'desc',
			_page: 1,
			_limit: num
		}
	})
}
// 按点赞数获取(热门文章)
// 按点赞数获取前num的文章
const getArticle_hot = (num, AuthorID) => {
	return axios({
		url: articlesWeb,
		method: 'get',
		params: {
			AuthorID: AuthorID,
			_sort: 'likeCount',
			_order: 'desc',
			_page: 1,
			_limit: num
		}
	})
}
// 全局搜索
const getArticle_serach = keyword => {
	return axios({
		url: articlesWeb,
		method: 'get',
		params: {
			q: keyword
		}
	})
}

// ====================获取评论接口==================== //
// 获取所有评论
const getComments_all = () => {
	return axios({
		url: commentsWeb,
		method: 'get'
	})
}
// 按文章ID获取该文章的所有评论
const getComments = articleID => {
	return axios({
		url: commentsWeb,
		method: 'get',
		params: {
			articleID: articleID
		}
	})
}
// 按日期获取最新num个评论
const getComment_new = (num, userID) => {
	return axios({
		url: commentsWeb,
		method: 'get',
		params: {
			userID: userID,
			_sort: 'date',
			_order: 'desc',
			_page: 1,
			_limit: num
		}
	})
}

// ====================写文章接口==================== //
// 发布文章
const publishArticle = article => {
	return axios({
		url: articlesWeb,
		method: 'post',
		data: article
	})
}
// 更新文章
const updateArticle = (articleID, article) => {
	return axios({
		url: articlesWeb_write + articleID,
		method: 'patch',
		data: article
	})
}
// 更新文章点赞、评论、分享数
const updateArticle_support = (articleID, support) => {
	return axios({
		url: articlesWeb_write + articleID,
		method: 'patch',
		data: {
			likeCount: support.likeCount,
			commentsCount: support.commentsCount,
			shareCount: support.shareCount
		}
	})
}
// 删除文章
const deleteArticle = articleID => {
	return axios({
		url: articlesWeb_write + articleID,
		method: 'delete'
	})
}

// ====================写评论接口==================== //
// 写评论
const publishComment = comment => {
	return axios({
		url: commentsWeb,
		method: 'post',
		data: comment
	})
}
// 评论回复
const publishReply = (id, reply) => {
	return axios({
		url: replyWeb + id,
		method: 'patch',
		data: {
			replys: reply
		}
	})
}

// ====================草稿箱接口==================== //
// 获取所有草稿
const getDraft_all = () => {
	return axios({
		url: draftWeb,
		method: 'get'
	})
}
// 根据用户id获取草稿
const getDraft = AuthorID => {
	return axios({
		url: draftWeb,
		method: 'get',
		params: {
			AuthorID: AuthorID
		}
	})
}
// 储存草稿
const saveDraft = draft => {
	return axios({
		url: draftWeb,
		method: 'post',
		data: draft
	})
}
// 更新草稿
const updateDraft = (draftID, draft) => {
	return axios({
		url: draftWeb + draftID,
		method: 'patch',
		data: draft
	})
}
// 删除草稿
const deleteDraft = draftID => {
	return axios({
		url: draftWeb + draftID,
		method: 'delete'
	})
}

// ====================图片接口==================== //
// 上传图片
const postPic = picFile => {
	return axios({
		url: postPicWeb,
		method: 'post',
		withCredentials: false,
		headers: {
			'Content-Type': 'multipart/form-data'
		},
		data: {
			file: picFile,
			strategy_id: 2
		}
	})
}
// ====================聊天接口==================== //
// 获取聊天记录
const getchat = () => {
	return axios({
		url: chatWeb,
		method: 'get'
	})
}
// 记录聊天记录
const recordingChat = chat => {
	return axios({
		url: chatWeb,
		method: 'post',
		data: chat
	})
}

export {
	getMainDisplay,
	updateMainDisplay,
	getHomeDisplay,
	updateHomeDisplay,
	postHomeDisplay,
	login,
	register,
	updateUserInfo,
	closeAccount,
	getArticle_all,
	getArticle_id,
	getArticle_author,
	getArticle_type,
	getArticle_tag,
	getArticle_date,
	getArticle_new,
	getArticle_hot,
	getArticle_serach,
	getComments_all,
	getComments,
	getComment_new,
	publishArticle,
	updateArticle,
	updateArticle_support,
	deleteArticle,
	publishComment,
	publishReply,
	getDraft_all,
	getDraft,
	saveDraft,
	updateDraft,
	deleteDraft,
	postPic,
	getchat,
	recordingChat
}
